<?php

use think\facade\Db;


function getBonusTitle($type)
{
    if ($type == 1) {
        return [1 => '推荐奖', 2 => '释放'];
    } else {
        return ['sum_1' => '推荐奖', 'sum_2' => '释放'];
    }

}

function getCount($fetchSql)
{
    $fetchSql = "SELECT count(*) as tp_count " . strstr($fetchSql, "FROM");
    return Db::query($fetchSql)[0]['tp_count'];
}

function get_pay_way($value)
{
    switch ($value) {
        case "":
            return '-';
        case "alipay":
            return '支付宝';
        case "wechat":
            return '微信支付';
        default:
            $assets = \app\common\model\Assets::getFields(3);
            return $assets[$value];
    }
}

function getOrderStatus($value, $type = 1)
{
    if ($type == 1) {
        switch ($value) {
            case 1:
                return '待付款';
            case 2:
                return '待发货';
            case 3:
                return '待收货';
            case 4:
                return '已完成';
            case 6:
                return '已取消';
            default:
                return '其他';
        }
    } else {
        return [1 => '待付款', 2 => '待发货', 3 => '待收货', 4 => '已完成', 6 => '已取消'];
    }

}

function getOperation($name)
{
    switch ($name) {
        case "save":
            return '修改';
        case "create":
            return '新增';
        case "update":
            return '更新状态';
        case "updateAll":
            return '批量更新状态';
        case "delete":
            return '删除';
        case "deleteAll":
            return '批量删除';
        default:
            return '操作';
    }
}

function getNotifyType($name)
{
    switch ($name) {
        case 1:
            return '订单通知';
        case 2:
            return '注册通知';
        default:
            return '系统通知';
    }
}

/**
 * 格式化字节大小
 * @param number $size 字节数
 * @param string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '')
{
    $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
    for ($i = 0; $size >= 1024 && $i < 5; $i++) {
        $size /= 1024;
    }
    return $size . $delimiter . $units[$i];
}


function generate($length, $prefix = '', $type = 1)
{
    if ($type == 1) {
        $code = rand(100000, 999999);
    } else {
        $chars = array('a', 'b', '1', 'd', 'e', '2', 'g', 'h',
            'i', '3', 'k', 'l', '4', 'n', 'o', '5', 'q', 'r', 's',
            't', 'u', '6', 'w', 'x', '7', 'z', 'c', '8', 'j', 'm',
            'p', 'v', 'y', 'f', '9');
        $keys = array_rand($chars, $length);
        $code = '';
        for ($i = 0; $i < $length; $i++) {
            $code .= $chars[$keys[$i]];
        }
    }

    if ($prefix) {
        $code = $prefix . $code;
    }

    $is = \app\common\model\User::where('code', $code)->count();
    if ($is) {
        return generate(6, $prefix, $type);
    } else {
        return $code;
    }

}

//格式化富文本内容
function handleRichText($content, $http = 'http')
{
    $url = $http . '://' . $_SERVER['SERVER_NAME'] . '/uploads/';
    $content = htmlspecialchars_decode(str_replace('/uploads/', $url, $content));
    return str_replace('\\', '', $content);
}

function handleImg($src, $http = 'https')
{
    if (empty($src)) {
        return '';
    }
    if (is_array($src)) {
        foreach ($src as &$v) {
            $v = $http . '://' . $_SERVER['SERVER_NAME'] . $v;
        }
        return $src;
    } else {
        return $http . '://' . $_SERVER['SERVER_NAME'] . $src;
    }

}

function filter_Emoji($str)
{
    $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str);

    return $str;
}

function queryIp($ip)
{

    $url = "http://whois.pconline.com.cn/jsFunction.jsp?ip=" . $ip;
    $ch = curl_init();
    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    $info = iconv('GB2312', 'UTF-8', $output); //因为是js调用 所以接收到的信息为字符串，注意编码格式

    return get_between($info, "('", "',");
}

function get_between($input, $start, $end)
{
    $substr = substr($input, strlen($start) + strpos($input, $start), (strlen($input) - strpos($input, $end)) * (-1));
    return $substr;
}

